<!DOCTYPE html>
<html>
<head>
    <title>分类编辑</title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ category.id }}" name="categoryId" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级分类</label>
                    <div class="layui-input-block">
                        <ul id="selectParent" name="parentId" class="dtree" data-id="-1"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ category.title }}" name="title" lay-verify="required"
                               autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">别名</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ category.alias }}" name="alias" lay-verify="required"
                               autocomplete="off" placeholder="请输入别名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入分类描述" name="desc"
                                  class="layui-textarea">{{ category.desc }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="category-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
    layui.use(['form', 'jquery', 'dtree'], function () {
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree

        dtree.renderSelect({
            elem: '#selectParent',
            url: '/admin/cms/category/selectParent?exclude_id={{ category.id }}',
            method: 'get',
            selectInputName: {nodeId: 'parentId', context: 'title'},
            skin: 'layui',
            dataFormat: 'list',
            response: {treeId: 'id', parentId: 'parent_id', title: 'title'},
            selectInitVal: "{{ category.parent_id or 0 }}",
            done: function(obj) {
                // 监听选择事件
                dtree.on("select('selectParent')", function(obj) {
                    let selectedId = obj.param.nodeId;
                    let currentId = "{{ category.id }}";
                    
                    // 检查是否选择了自己作为父级（双重保险）
                    if (selectedId == currentId) {
                        layer.msg('不能选择自己作为父级分类', {icon: 2, time: 2000});
                        // 重置选择为顶级分类
                        dtree.selectVal('selectParent', '0');
                        return false;
                    }
                });
            }
        })

        form.on('submit(category-save)', function (data) {
            // 前端验证：检查是否选择自己作为父级
            let selectedParentId = data.field.parentId;
            let currentId = "{{ category.id }}";
            
            if (selectedParentId == currentId) {
                layer.msg('不能选择自己作为父级分类', {icon: 2, time: 2000});
                return false;
            }
            
            $.ajax({
                url: '/admin/cms/category/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.render()
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>
</body>
</html> 